using System;
using System.Collections.Generic;
using System.Text;

namespace Fuzzy
{
    public class TriangularFuzzySet:PieceWiseLinearFuzzySet
    {
        public TriangularFuzzySet(FuzzyLinguisticVariable fuzzyLinguisticVariable, double[] peakPositions, double[] peakValues)
            : base(fuzzyLinguisticVariable, peakPositions, peakValues)
        {
            if (peakPositions.Length != 3)
                throw new FuzzyException("Number of peaks should be 3.");

            if ((peakValues[0] != peakValues[2]) || (peakValues[0] >= peakValues[1]))
                throw new FuzzyException("It's not triangular.");


        }

        public TriangularFuzzySet(FuzzyLinguisticVariable fuzzyLinguisticVariable, string name, double[] peakPositions, double[] peakValues)
            : base(fuzzyLinguisticVariable, name, peakPositions, peakValues)
        {
            if (peakPositions.Length != 3)
                throw new FuzzyException("Number of peaks should be 3.");

            if ((peakValues[0] != peakValues[2]) || (peakValues[0] >= peakValues[1]))
                throw new FuzzyException("It's not triangular.");
        }

        public override double Membership(double x)
        {
            if ((x <= fuzzyLinguisticVariable.DomainMin) || (x > fuzzyLinguisticVariable.DomainMax))
                return 0.0;
            if ((x <= peakPositions[0]) || (x > peakPositions[2]))
                return peakValues[0];
            if (x <= peakPositions[1])
                return peakValues[0]+(x-peakPositions[0])*(peakValues[1]-peakValues[0])/(peakPositions[1]-peakPositions[0]);
            if (x > peakPositions[1])
                return peakValues[1] + (x - peakPositions[1]) * (peakValues[2] - peakValues[1]) / (peakPositions[2] - peakPositions[1]);
            return 0.0;
        }


    }
}
